﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using BHair.Report;

namespace BHair.Report
{
    public partial class frmycxmbb : WeifenLuo.WinFormsUI.Docking.DockContent
    {
        public frmycxmbb()
        {
            InitializeComponent();
        }
        string constr = ConfigurationManager.ConnectionStrings["BHair.Properties.Settings.BHairDBConnectionString"].ConnectionString;
        private void frmycxmbb_Load(object sender, EventArgs e)
        {
            jzxm();
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string sql = "SELECT distinct [cardID] as 卡号 ,member.mname as 姓名 ,[jcspid] as 项目编号,items.iname as 项目名称 ,[jcnum] as 预存次数,date1 as 充值日期,bz as 备注 FROM [ycxm] left join member on ycxm.cardID=member.mid join items on items.iid=ycxm.jcspid";
            SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            dataGridView1.DataSource = dt;
            conn.Close();
        }
        public void jzxm()
        {
            DataSet ds = new DataSet();
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string sql = "SELECT [id],[iid] ,[iname] FROM [items] where jc=1";
            SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
            sda.Fill(ds, "items");
            comboBox1.DataSource = ds.Tables["items"];
            comboBox1.DisplayMember = "iname";
            comboBox1.SelectedIndex = -1;
            conn.Close();

        }
        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string sql="";
            if (comboBox1.Text == string.Empty)
            {
                sql =string.Format("SELECT distinct [cardID] as 卡号 ,member.mname as 姓名 ,[jcspid] as 项目编号,items.iname as 项目名称 ,[jcnum] as 预存次数,date1 as 充值日期,bz as 备注 FROM [ycxm] left join member on ycxm.cardID=member.mid left join items on items.iid=ycxm.jcspid where date1>='{0}' and date1<='{1}'", dtpbegin.Text,dtpend.Text);
            }
            else
            {
                sql =string.Format("SELECT distinct [cardID] as 卡号 ,member.mname as 姓名 ,[jcspid] as 项目编号,items.iname as 项目名称 ,[jcnum] as 预存次数,date1 as 充值日期 ,bz as 备注 FROM [ycxm] left join member on ycxm.cardID=member.mid left join items on items.iid=ycxm.jcspid where items.iname='{0}' and date1>='{1}' and date1<='{2}'", comboBox1.Text,dtpbegin.Text,dtpend.Text);
            }
            SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            dataGridView1.DataSource = dt;
            conn.Close();
        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
        e.RowBounds.Location.Y,
        dataGridView1.RowHeadersWidth - 4,
        e.RowBounds.Height);

            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
            dataGridView1.RowHeadersDefaultCellStyle.Font,
                rectangle,
                dataGridView1.RowHeadersDefaultCellStyle.ForeColor,
                TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
            saveFileDialog.FilterIndex = 0;
            saveFileDialog.RestoreDirectory = true;
            saveFileDialog.CreatePrompt = true;
            saveFileDialog.Title = "导出Excel文件到";

            saveFileDialog.ShowDialog();

            // 这个一定要加上，要不然，点取消按钮就会提示“索引超出了数组界限”的错误。
            //用saveFileDialog.ShowDialog() == DialogResult.Cancle 不管用，会出现2次存储窗口。
            if (saveFileDialog.FileName == "")
            {

                return;
            }


            Stream myStream;
            myStream = saveFileDialog.OpenFile();
            StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312"));
            string str = "";
            try
            {
                //写标题       
                for (int i = 0; i < dataGridView1.ColumnCount; i++)
                {
                    if (i > 0)
                    {
                        str += "\t";
                    }
                    str += dataGridView1.Columns[i].HeaderText;
                }
                sw.WriteLine(str);
                //写内容    
                for (int j = 0; j < dataGridView1.Rows.Count; j++)
                {
                    string tempStr = "";
                    for (int k = 0; k < dataGridView1.Columns.Count; k++)
                    {
                        if (k > 0)
                        {
                            tempStr += "\t";
                        }
                        if (dataGridView1.Rows[j].Cells[k].Value != null)
                        {

                            tempStr += dataGridView1.Rows[j].Cells[k].Value.ToString();
                        }
                        else
                        {
                            tempStr += "";
                        }
                    }
                    sw.WriteLine(tempStr);
                }
                sw.Close();
                myStream.Close();
                MessageBox.Show("导出成功");
            }
            catch (Exception err)
            {
                MessageBox.Show(err.ToString());
            }
            finally
            {
                sw.Close();
                myStream.Close();
            }
        }
    }
}
